#include "ModuleFactory.h"

#ifndef _DONOT_NEED_DLLENTERY
BOOL APIENTRY DllMain(__in void * _HDllHandle, __in unsigned _Reason, __in_opt void * _Reserved)
{
	_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
	return TRUE;
}
#endif

std::map<std::string, CREATORFUNC> &GetClassDeposit()
{
	static std::map<std::string, CREATORFUNC> mapClassCreator;
	return mapClassCreator;
}

IModule * CreateModuleObject(const char *lpzClassName)
{
	IModule *pModule = NULL;
	std::map<std::string, CREATORFUNC>::iterator it = GetClassDeposit().find(lpzClassName);
	if (it != GetClassDeposit().end())
	{
		CREATORFUNC pfnCreate = it->second;
		pModule = pfnCreate();
	}
	return pModule;
}

bool GetClassNames(char **lpzClassName, size_t &nCount)
{
	if (lpzClassName == NULL)
	{
		nCount = GetClassDeposit().size();
	}
	else
	{
		std::map<std::string, CREATORFUNC>::iterator it = GetClassDeposit().begin();
		int i = 0;
		while (it != GetClassDeposit().end())
		{
			const std::string sName = it->first;
			strcpy_s(lpzClassName[i], 50, sName.c_str());
			it++;
			i++;
		}
	}
	return 1;
}
